var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
//逛街10次
var shop_10 = {
  is_over_today: false,
  has_finished_times: 0,
  go_page_from_home() {
    var logger = utils.create_logger("shop_10 go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["首页", "发现"], true)) {
      logger("");
      // utils.click_point.apply(
      //   utils,
      //   utils.get_size_point(point_map["首页任务"])
      // );
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      if (this.is_over_today) {
        logger("今天的逛街任务已经全部完成了");
        this.next();
        return;
      }
      this.init();
    } else {
      logger("NOT FOUND");
    }
  },
  init() {
    in_task.init();
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    var logger = utils.create_logger("shop_10 init: ");
    var ocr_text = ocr_utils.screen_exists_text_one("逛街", true);
    if (ocr_text) {
      utils.click_bounds(ocr_text.bounds);
      logger("找到-逛街-");
      sleep(utils.get_random_interval(4, 0, 1, 1000));
      if (!ocr_utils.screen_exists_text_one("浏览本页")) {
        logger("没找到-浏览本页-");
        this.is_over_today = true;
        this.after();
        return;
      }
      this.run();
    } else {
      logger("没找到-逛街-");
    }
  },
  run() {
    var logger = utils.create_logger("shop_10 run: ");
    for (var index = 0; index < 1; index++) {
      var search_swipe_times = 12 + random(0, 2);
      logger(
        "即将开始第-" +
          (index + 1) +
          "-次逛街，需要滑动-" +
          search_swipe_times +
          "-次"
      );
      var up_times = 0;
      var down_times = 0;
      var max_up_times = utils.get_random_interval(2, 0, 1, 1);
      logger("最多向上滑动-" + max_up_times + "-次");
      while (search_swipe_times > 0) {
        if (up_times < max_up_times) {
          utils.swipe(1 / 2, 5 / 10, 1 / 2, 2 / 10);
          up_times = up_times + 1;
          down_times = down_times - 1;
          logger("已经向上滑动-1-次");
        } else {
          utils.swipe(1 / 2, 5 / 10, 1 / 2, 8 / 10);
          down_times = down_times + 1;
          up_times = up_times - 1;
          logger("已经向下滑动-1-次");
        }
        var watch_search_result_time = 5 + random(0, 2);
        sleep(watch_search_result_time * 1000);
        search_swipe_times = search_swipe_times - 1;
        if (search_swipe_times == 0) {
          logger("第" + (index + 1) + "次逛街已完成");
        } else {
          logger("还需要滑动" + search_swipe_times + "次");
        }
        if (search_swipe_times < 5) {
          if (ocr_utils.screen_exists_text_one(["开心收下", "看视频再得"])) {
            logger("找到-开心收下或看视频再得-，可以结束滑动了！");
            break;
          }
        }
      }
      var ocr_text = ocr_utils.screen_exists_text_one("开心收下", false);
      if (ocr_text) {
        logger("找到-开心收下-");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        continue;
      }
      var ocr_text = ocr_utils.screen_exists_text_one("看视频再得", false);
      if (ocr_text) {
        logger("找到-看视频再得-");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        ad.init(
          function () {
            var result = utils.exists_text_starts_with("领取成功", false);
            if (result) {
              logger("找到-领取成功-");
            }
            return result;
          },
          function () {
            if (utils.exists_text_starts_with("恭喜你获得")) {
              logger("找到-恭喜你获得-");
              return true;
            }
            var ocr_text = ocr_utils.screen_exists_text_one("逛街", false);
            if (ocr_text) {
              logger("找到-逛街-");
              return true;
            }
          },
          function () {
            return ocr_utils.screen_exists_text_all(["首页", "发现"], false);
          },
          ["看视频再得"]
        );
        if (ad.in_task) {
          utils.click_point.apply(
            utils,
            utils.get_size_point(point_map["恭喜你获得之后的关闭"], 1080, 2230)
          );
          sleep(utils.get_random_interval(2, 0, 1, 1000));
        }
        continue;
      }
      continue;
    }
    this.has_finished_times = this.has_finished_times + 1;
    if (this.has_finished_times == 10) {
      this.is_over_today = true;
      logger("今天的逛街任务全部完成了");
    } else {
      logger("今天已经逛街-" + this.has_finished_times + "-次");
    }
    this.after();
  },
  after() {
    if (utils.click_text_starts_with("任务")) {
      sleep(utils.get_random_interval(2, 0, 1, 1000));
      in_task.init();
    }
    console.log("shop_10 after");
  },
  next() {
    console.log("shop_10 next");
    console.hide();
  },
};
module.exports = shop_10;
